package com.vcredit.creditcat.web;

import com.vcredit.creditcat.enumtype.ChannelOnlineStatusEnum;
import com.vcredit.creditcat.framework.pagination.DefaultCriteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.*;
import com.vcredit.creditcat.service.ChannelBusinessStatusService;
import com.vcredit.creditcat.service.RegisterChannelService;
import com.vcredit.creditcat.service.ResourceService;
import com.vcredit.creditcat.service.RoleService;
import com.vcredit.creditcat.vo.BuiSelectVo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by liaoshengjun on 2017/10/24.
 */
@Controller
@RequestMapping("/registerChannel")
public class RegisterChannelController extends BaseController{
	@Autowired
	private RegisterChannelService registerChannelService;
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ChannelBusinessStatusService channelBusinessStatusService;

	@RequestMapping("/list")
	public ModelAndView showPageRegisterChannel(HttpServletRequest request) {
		return new ModelAndView("/registerchannel/registerChannelList");
	}
	@ResponseBody
	@RequestMapping("/listRegisterChannel")
	public Page<RegisterChannel> findByPageRegisterChannel(HttpServletRequest request) {
		DefaultCriteria param=makeDefaultCriteria(request);
		Page<RegisterChannel> result=registerChannelService.findByPage(param);
		return result;
	}

	@ResponseBody
	@RequestMapping("/listAllChannels")
	public List<BuiSelectVo> listAllChannel(HttpServletRequest request) {
		return registerChannelService.listAllChannel();
	}

	@ResponseBody
	@RequestMapping("/saveRegisterChannel")
	@Transactional
	public String saveRegisterChannel(@RequestBody RegisterChannel registerChannel, HttpServletRequest request) {

		RegisterChannel registerChannelSearch=new RegisterChannel();
		registerChannelSearch.setChannel(registerChannel.getChannel());
		//判断当前新增渠道已存在
		List<?> list =  registerChannelService.find(registerChannelSearch);
		if(list.size()>0) {
			return "exit";
		}

		//新增渠道
		registerChannelService.insert(registerChannel);
		//保存渠道商务状态信息
		saveChannelBusinessStatus(registerChannel);

		Resource resource=new Resource();
		resource.setName(registerChannel.getChannelDesc());
		resource.setValue(registerChannel.getChannel());
		resource.setResourceType(2);



		//新增渠道数据查询权限
		resourceService.insert(resource);
		Role role=new Role();
		role.setRoleName(registerChannel.getChannel());
		role.setDesc(registerChannel.getChannelDesc());
		//新增渠道角色
		roleService.insert(role);
		if(resource.getId()!=null && role.getRoleId()!=null){
			RoleResource roleResource=new RoleResource();
			roleResource.setResourceId(resource.getId());
			roleResource.setRoleId(role.getRoleId());
			//新增渠道角色数据查询权限
			roleService.insertRoleResource(roleResource);
		}
		Resource menuParam1=new Resource();
		menuParam1.setMenuId("channel");
		menuParam1.setResourceType(0);
		menuParam1.setpId(0L);
		//查询渠道管理主菜单
		List<Resource> menus=resourceService.find(menuParam1);
		if(menus!=null && menus.size()>0){
			RoleResource roleMenu=new RoleResource();
			roleMenu.setResourceId(menus.get(0).getId());
			roleMenu.setRoleId(role.getRoleId());
			//开通角色渠道主菜单查看权限
			roleService.insertRoleResource(roleMenu);
			Resource menuParam2=new Resource();
			menuParam2.setMenuId("channelCountList");
			menuParam2.setpId(menus.get(0).getId());
			menuParam2.setResourceType(0);
			//查询渠道导流统计菜单
			List<Resource> childMenus=resourceService.find(menuParam2);
			if(childMenus!=null && childMenus.size()>0){
				RoleResource roleChildMenu=new RoleResource();
				roleChildMenu.setResourceId(childMenus.get(0).getId());
				roleChildMenu.setRoleId(role.getRoleId());
				//开通角色渠道导流统计菜单查看权限
				roleService.insertRoleResource(roleChildMenu);
			}
		}
		return SUCCESS;
	}
	@ResponseBody
	@RequestMapping("/updateRegisterChannel")
	@Transactional
	public String recommendCardUpdate(@RequestBody RegisterChannel registerChannel,HttpServletRequest request) {
		registerChannelService.update(registerChannel);
		saveChannelBusinessStatus(registerChannel);
		return SUCCESS;
	}
	@ResponseBody
	@RequestMapping("/listAllOnlineStatus")
	public List<BuiSelectVo> listAllOnlineStatus(HttpServletRequest request) {
		List<BuiSelectVo> onLineStatusList=new ArrayList<>();
		for(ChannelOnlineStatusEnum onlineStatusEnum:ChannelOnlineStatusEnum.values()){
			BuiSelectVo item=new BuiSelectVo();
			item.setText(onlineStatusEnum.getDesc());
			item.setValue(String.valueOf(onlineStatusEnum.getCode()+1));
			onLineStatusList.add(item);
		}
		return onLineStatusList;
	}

	/**
	 * 保存渠道商务状态信息
	 * @param registerChannel
	 */
	private void saveChannelBusinessStatus(RegisterChannel registerChannel){
		ChannelBusinessStatus channelBusinessStatus=new ChannelBusinessStatus();
		if(registerChannel.getOnlineStatus()!=null){
			channelBusinessStatus.setOnlineStatus(registerChannel.getOnlineStatus()-1);
		}
		channelBusinessStatus.setChannelBusinessId(registerChannel.getChannelBusinessId());
		channelBusinessStatus.setChannel(registerChannel.getChannel());
		channelBusinessStatus.setBusinessStartDate(registerChannel.getBusinessStartDate());
		channelBusinessStatus.setContractStartDate(registerChannel.getContractStartDate());
		channelBusinessStatus.setContractEndDate(registerChannel.getContractEndDate());
		channelBusinessStatus.setPrepareOnlineDate(registerChannel.getPrepareOnlineDate());
		channelBusinessStatus.setOnlineStartDate(registerChannel.getOnlineStartDate());
		channelBusinessStatus.setOnlineEndDate(registerChannel.getOnlineEndDate());
		channelBusinessStatus.setRemarks(registerChannel.getRemarks());
		if(channelBusinessStatus!=null && channelBusinessStatus.getChannelBusinessId()!=null){
			channelBusinessStatusService.update(channelBusinessStatus);
		}else{
			channelBusinessStatusService.save(channelBusinessStatus);
		}

	}
}
